package com.c611.smartclassroom.controller;

import com.aliyuncs.exceptions.ClientException;
import com.c611.smartclassroom.ali.ali;
import com.c611.smartclassroom.common.DataGridView;
import com.c611.smartclassroom.dto.DeviceDto;
import com.c611.smartclassroom.model.Device;
import com.c611.smartclassroom.model.DeviceVo;
import com.c611.smartclassroom.model.GateWay;
import com.c611.smartclassroom.model.Scene;
import com.c611.smartclassroom.service.DeviceService;
import com.c611.smartclassroom.service.GateWayService;
import com.c611.smartclassroom.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("device")
public class DeviceController {
    @Autowired
    DeviceService deviceService;
    @Autowired
    GateWayService gateWayService;
    @Autowired
    SchoolService schoolService;

    /**
     * 根据搜索信息查询所有教室信息
     * @param buildId 教学楼ID必填
     * @param status 在线状态可不填
     * @param gateWayMac 网关Mac可不填
     * @return
     */
    @PostMapping("queryDevice")
    public DataGridView<DeviceDto> queryDevice(String buildId, @RequestParam(value = "status",required = false,defaultValue = "-1")
            int status, @RequestParam(value = "gateWayMac",required = false) String gateWayMac){
        if (buildId == null) return null;
        List<DeviceDto> deviceDtoList = deviceService.queryDeviceDtoByBuildId(buildId,status,gateWayMac);
        if(deviceDtoList == null || deviceDtoList.size() <= 0) return null;
        return new DataGridView<DeviceDto>(0,"ok",(long)(deviceDtoList.size()),deviceDtoList);
    }

    /**
     * 根据点击的教学楼查询所有教室信息
     * @param buildId
     * @return DeviceDto列表
     */
    /*@PostMapping("queryDeviceDtoByBuildId")
    public List<DeviceDto> queryDeviceDtoByBuildId(String buildId){
        if (buildId == null) return null;
        return deviceService.queryDeviceDtoByBuildId(buildId);
    }*/
    /**
     * 根据场景id查询所有设备设置
     * @param sceneId
     * @return 模式信息
     */
    @PostMapping("querySceneById")
    public Scene querySceneById(@RequestParam(value = "sceneId",defaultValue = "-1") int sceneId){
        if (sceneId == -1) return null;
        return deviceService.querySceneById(sceneId);
    }

    /**
     * 根据网关ID查询该网关下的所有设备
     * @param gateWayId
     * @return
     */
    @PostMapping("queryDeviceByGateId")
    public DataGridView<Device> queryDeviceByGateId(String gateWayId){
        if (gateWayId == null) return null;
        List<Device> deviceList = deviceService.queryDeviceByGateId(gateWayId);
        return new DataGridView<Device>(0,"ok",(long)(deviceList.size()),deviceList);
    }
    /**
     * 根据教室ID查询该教室的所有设备
     * @param classroomId
     * @return 该教室下的所有设备
     */
    @PostMapping("queryDeviceByClassId")
    public List<Device> queryDeviceByClassId(String classroomId) {
        if(classroomId == null) return null;
        return deviceService.queryDeviceByClassId(classroomId);
    }

    /**
     * 根据设备ID查询该设备的所有参数
     * @param eqId
     * @return
     */
    @PostMapping("queryDataByDeviceId")
    public Device queryDataByDeviceId(String eqId) {
        if (eqId == null) return null;
        return deviceService.queryDataByDeviceId(eqId);

    }

    /**
     * 增加设备
     * @param device
     * @return
     */
    @PostMapping("addDevice")
    public int addDevice(Device device) {
        //确认不能为空的字段有数据
        if (device == null || device.getAfgateway() == null || device.getEqid() == null || device.getSchoolid() == null ||
                device.getTbuildingid() == null || device.getClassroomid() == null){
            return 0;
        }
        //新添设备可能主键相同
        try{
            //记录添加状态
            int result = deviceService.addDevice(device);
            //增加失败，直接返回0
            if (result == 0) return 0;
            //获取新增设备的网关id
            String gateWayId = device.getAfgateway();
            //查询该设备的网关是否存在
            GateWay gateWay = gateWayService.queryGateWayByGateWayId(gateWayId);
            //将该网关的设备数加1
            if (gateWay != null){
                gateWay.setEqnumber(gateWay.getEqnumber() + 1);
                gateWayService.saveGateWay(gateWay);
            }
            return result;
        }catch (Exception e){
            return 2;
        }
    }

    /**
     * 编辑设备信息
     * @param device
     * @return
     */
    @PostMapping("saveDevice")
    public int saveDevice(Device device) {
        if (device == null || device.getEqid() == null) return 0;
        //查询是否存在该设备
        Device device1 = deviceService.queryDataByDeviceId(device.getEqid());
        if (device1 == null) return 0;
        return deviceService.saveDevice(device);
    }

    /**
     * 删除设备
     * @param eqId
     * @return
     */
    @PostMapping("delDevice")
    public int delDevice(String eqId) {
        if (eqId == null) return 0;
        //查询是否存在该设备
        Device device = deviceService.queryDataByDeviceId(eqId);
        if (device == null) return 0;
        //删除结果
        int result = deviceService.delDevice(eqId);
        if(result == 0) return 0;
        //查询该网关是否存在
        GateWay gateWay = gateWayService.queryGateWayByGateWayId(device.getAfgateway());
        if (gateWay != null){
            //将该网关的设备数减1
            gateWay.setEqnumber(gateWay.getEqnumber() - 1);
            gateWayService.saveGateWay(gateWay);
        }

        return result;
    }

    @PostMapping("/setProperty")
    public int setProperty(String eqid,String propertyname,String propertyvalue) throws ClientException {
        //yw 根据eqid找到设备三元组并发送设置属性值topic到设备

        //ly 根据deviceID查询设备的IotId
        int eqID  = Integer.parseInt(eqid);
        DeviceMessageController deviceMessageController = new DeviceMessageController();
        String iotid = deviceMessageController.queryDeviceMessage(eqID).get(0).getIotid();


        //ly 设置设备的属性值
        ali ali = new ali();
        ali.SetDevicePropertyByIotId(iotid,propertyname,propertyvalue);

        return 1;
    }

    /**
     * 根据教学楼ID和教室ID查询该教室的所有设备
     * @param buildId,classroomId,controlMode
     * @return 该教室下的所有设备
     */
    @PostMapping("queryDevicesByTbuildIDClassroomId")
    public Object queryDevicesByTbuildIDClassroomId(String buildId,String classroomId,String controlMode) {
        //if(buildId == null ||classroomId == null ) return null;
        long count = deviceService.queryDevicesByTbuildIDClassroomIdCount(buildId,classroomId);
        List<DeviceVo> deviceList = deviceService.queryDevicesByTbuildIDClassroomId(buildId,classroomId,controlMode);
        return new DataGridView<DeviceVo>(0,"ok",count,deviceList);
    }


}
